use std::collections::HashMap;
use std::sync::{Arc, Mutex};
use std::thread;

fn main() {
    let shared_map = Arc::new(Mutex::new(HashMap::new()));
    // 管理线程
    let mut handles = vec![];
    for i in 0..5 {
        let shared_map = Arc::clone(&shared_map);
        let handle = thread::spawn(move || {
            //获取锁
            let mut map = shared_map.lock().unwrap();
            // 修改HashMap
            map.insert(i, i * i);
        });
        handles.push(handle);
    }

    //等待写入线程完成
    for handle in handles {
        handle.join().unwrap();
    }

    // 获取 HashMap，并输出结果
    let final_vec = shared_map.lock().unwrap();
    println!("Final Vec: {:?}", *final_vec);
}
